package cn.senjoeson.triper.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.senjoeson.mylibrary.utils.DisplayUtils;
import cn.senjoeson.triper.R;

/**
 * @author Android
 * @date 2018/1/5
 */

public class CustomLinearlayout extends LinearLayout {

    public ImageView mImage;
    public TextView mTextView;

    public CustomLinearlayout(Context context) {
        this(context, null, 0);
    }

    public CustomLinearlayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomLinearlayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View view = View.inflate(context, R.layout.each_categories, this);
        mImage = view.findViewById(R.id.image);
        mTextView = view.findViewById(R.id.text);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomLinearlayout);
/*<attr name="image_shape_bgcolor" format="integer"/>
        <attr name="text_color" format="color"/>
        <attr name="text_content" format="string"/>
        <attr name="text_size" format="integer"/>*/
        int image_shapecolor = attributes.getColor(R.styleable.CustomLinearlayout_image_shape_bgcolor, getResources().getColor(R.color.head_color));
      //  int textcolor = attributes.getColor(R.styleable.CustomLinearlayout_text_color, Color.BLACK);
        String textcontent = attributes.getString(R.styleable.CustomLinearlayout_text_content);
        int text_size = attributes.getInt(R.styleable.CustomLinearlayout_text_size, DisplayUtils.px2sp(14));

        GradientDrawable background = (GradientDrawable) mImage.getBackground();
        background.setColor(image_shapecolor);

        mTextView.setTextColor(image_shapecolor);
        mTextView.setText(textcontent);
        mTextView.setTextSize(text_size);


        attributes.recycle();
    }



    public String getTitle(){
        return mTextView.getText().toString().trim();
    }
}
